package com.cat.model.biz.service.admin.impl;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.core.commons.tuple.TwoTuple;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.service.admin.ModelInfoService;
import com.cat.model.common.model.common.CompanyInfoVO;
import com.cat.model.common.model.common.ModelExperVO;
import com.cat.model.common.model.common.ModelInfoVO;
import com.cat.model.common.model.common.ModelScalePictureVO;
import com.cat.model.common.model.common.ModelStyleVO;
import com.cat.model.common.model.common.ModelZedCardVO;
import com.cat.model.common.util.FileUtil;
import com.cat.model.common.util.StaticFileUtil;
import com.cat.model.dao.mapper.ModelMapper;

@Service("modelInfoService")
public class ModelInfoServiceImpl implements ModelInfoService{
	@Autowired
	private TxCommonDAO commonDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Override
	public ModelInfoVO createModelInfo(ModelInfoVO modelInfo,String[] style,String[] exper,File coverImage, String coverImageFileName){
		if(coverImage != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(coverImage);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, coverImageFileName, md5AndData.second);
			modelInfo.setCoverImage(imagePath + "/" + coverImageFileName);
		}
		Long id=commonDAO.insert(modelInfo);
		CompanyInfoVO companyInfoVO=modelMapper.getCompanyInfo(modelInfo.getCompanyId());
		modelInfo.setModelCode(companyInfoVO.getName()+id);
		commonDAO.updateById(modelInfo, "modelCode");
		if(style!=null&&style.length>0){
			for (int i = 0; i < style.length; i++) {   
				ModelStyleVO styleVO = new ModelStyleVO();
				styleVO.setModelId(modelInfo.getId());
				styleVO.setStyleCode(style[i]);
				commonDAO.insert(styleVO);
			}
		}
		
		if(exper!=null&&exper.length>0){
			for (int i = 0; i < exper.length; i++) {   
				ModelExperVO experVO = new ModelExperVO();
				experVO.setModelId(modelInfo.getId());
				experVO.setExperCode(exper[i]);
				commonDAO.insert(experVO);
			}
		}
		
		return modelInfo;
	}
	
	@Override
	public int deleteModelInfo(long id){
		ModelInfoVO modelInfo = new ModelInfoVO();
		modelInfo.setId(id);
		return commonDAO.deleteById(modelInfo);
	}
	
	@Override
	public int updateModelInfo(ModelInfoVO modelInfo,String[] style,String[] exper,File coverImage, String coverImageFileName){
		if(coverImage != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(coverImage);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, coverImageFileName, md5AndData.second);
			modelInfo.setCoverImage(imagePath + "/" + coverImageFileName);
		}
		
		List<ModelStyleVO> styles = modelMapper.getModelStyleList(modelInfo.getId());
		if(styles!=null&&styles.size()>0){
			for (int i = 0; i < styles.size(); i++) {   
				ModelStyleVO styleVO = (ModelStyleVO)styles.get(i);
				if(null!=styleVO){
					commonDAO.deleteById(styleVO);
				}
			}
		}
		
		List<ModelExperVO> expers = modelMapper.getModelExperList(modelInfo.getId());
		if(expers!=null&&expers.size()>0){
			for (int i = 0; i < expers.size(); i++) {   
				ModelExperVO experVO = (ModelExperVO)expers.get(i);
				commonDAO.deleteById(experVO);
			}
		}
		
		if(style!=null&&style.length>0){
			for (int i = 0; i < style.length; i++) {   
				ModelStyleVO styleVO = new ModelStyleVO();
				styleVO.setModelId(modelInfo.getId());
				styleVO.setStyleCode(style[i]);
				commonDAO.insert(styleVO);
			}
		}
		
		if(exper!=null&&exper.length>0){
			for (int i = 0; i < exper.length; i++) {   
				ModelExperVO experVO = new ModelExperVO();
				experVO.setModelId(modelInfo.getId());
				experVO.setExperCode(exper[i]);
				commonDAO.insert(experVO);
			}
		}
		
		return commonDAO.updateById(modelInfo, "modelCode","name","sex","age","coverImage","price","countryCode","provinceCode","cityCode", "height", "weight",
																					"shoeSize","bustSize","hiplineSize","waistSize","cupSize","companyId","richResume","skinColorCode","remark",
																					"hairColorCode","raceCode","eyeCode","armLength","upArmSize","legSize","thighSize","ageGroup","neckLength","neckSize",
																					"headSize","collarSize","shoulderSize","annLength","sleeveLength","centerBack","chestSize","underbustSize","highHipSize","midHipSize",
																					"lowHipSize","wristSize","outseamSize","inseamSize","calfSize","ankleSize","suitSizes","endTime","beginTime");
	}
	
	@Override
	public ModelScalePictureVO addModelScalePic(ModelScalePictureVO vo,
			File image, String imageFileName, File vedio, String vedioFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		if(vedio != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(vedio);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, vedioFileName, md5AndData.second);
			vo.setVedio(imagePath + "/" + vedioFileName);
		}
		
		commonDAO.insert(vo);
		return vo;
	}
	
	@Override
	public int deleteModelScalePic(long id){
		ModelScalePictureVO vo = new ModelScalePictureVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}

	@Override
	public int updateModelScalePic(ModelScalePictureVO vo,
			File image, String imageFileName, File vedio, String vedioFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		if(vedio != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(vedio);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, vedioFileName, md5AndData.second);
			vo.setVedio(imagePath + "/" + vedioFileName);
		}
		return commonDAO.updateById(vo, "mateType","vedio","image");
	}
	
	@Override
	public ModelZedCardVO addModelZedCard(ModelZedCardVO vo,File image, String imageFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		commonDAO.insert(vo);
		return vo;
	}
	
	@Override
	public int deleteModelZedCard(long id){
		ModelZedCardVO vo = new ModelZedCardVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}

	@Override
	public int updateModelZedCard(ModelZedCardVO vo,File image, String imageFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.modelInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		return commonDAO.updateById(vo, "image");
	}
}
